<?php

/**
 * This is the model class for table "tbl_contract_status".
 *
 * The followings are the available columns in table 'tbl_contract_status':
 * @property integer $id
 * @property string $description
 * @property integer $inactive
 * @property integer $sort
 */
class ContractStatus extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @return ContractStatus the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'tbl_contract_status';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('inactive, sort', 'numerical', 'integerOnly' => true),
            array('description', 'length', 'max' => 36),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, description, inactive, sort', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'description' => 'Description',
            'inactive' => 'Inactive',
            'sort' => 'Sort',
        );
    }

    public static function getText($id){
        $text=Yii::app()->db->createCommand('select description from tbl_contract_status where id=:id')
                ->bindValue('id', $id)->queryScalar();
        return $text;
    }


    public static function getListData(){
        $criteria = new CDbCriteria;
        $criteria->order = 'sort ASC';
        $criteria->condition = 'inactive=0';
        $list=ContractStatus::model()->findAll($criteria);
        $rs=array(''=>'N/A');
        if(count($list)>0){
            foreach($list as $item){
                $rs[$item->id]=$item->description;
            }
        }
        return $rs;
    }    
    
       public static function getContractStatusListAndCount($empno=false){
            $list=self::getListData();
            unset($list['']);
            
            $rs=array();
            
         
            foreach($list as $id=>$status){
                /*if($empno)
                    $count=Yii::app()->db->createCommand('select count(*)
                                                         from tbl_jobs
                                                         where jobstatus=:status and jobsalesperson=:empno')
                        ->bindValues(array('status'=> $id,'empno'=>$empno))->queryScalar();
                    else*/
                $count=Yii::app()->db->createCommand('select count(*) from tbl_contracts where status=:status')
                        ->bindValue('status', $id)->queryScalar();
                if($count!==false && $count!=null){
                    $rs[$status]=$count;
                }else{
                    $rs[$status]=0;
                }
            }
        
            return $rs;
        }    
        
}